/*
 * @(#)NoticeAction.java $version 2012-8-9
 * 
 * Copyright 2012 NADOSTAR Gruop. All rights Reserved.
 * NADOSTAR PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nadostar.iims.etc.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nadostar.extensions.CommonUtil;
import com.nadostar.extensions.pagination.PaginationInfo;
import com.nadostar.extensions.util.DataMap;
import com.nadostar.iims.etc.bo.CommBO;
import com.nadostar.iims.etc.bo.NoticeBO;
import com.nadostar.iims.model.Notice;

/**
 * The Class NoticeAction.
 * 
 * @author Lixing
 *
 */
@Controller
@RequestMapping("/etc/notice")
public class NoticeAction {
	private static Log log = LogFactory.getLog(NoticeAction.class);
	
	private static final String KEY_COMMCD_NOTICE = "NOTICE";
	
	private static PaginationInfo paginate = null;
	
	@Autowired
	private NoticeBO noticeBO = null;
	
	@Autowired
	private CommBO commBO = null;
	
	@ModelAttribute
	public void modelPaginateAttr(HttpServletRequest request) {
		if(paginate == null) {
			paginate = new PaginationInfo("default", request, 0);
		}
	}
	
	@ModelAttribute
	public void modelNoticeTypeListAttr(Model model) {
		log.debug("### NoticeAction.modelNtcTypeCdList...");
		model.addAttribute("noticeTypeList", commBO.getCommByCommCdCatList(KEY_COMMCD_NOTICE));
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String noticeList() {
		return "etc/noticeList";
	}
	
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public @ResponseBody Map<String, ? extends Object> search(
			@RequestParam("page") int page,
			@RequestParam("ntcTypeCd") String ntcTypeCd,
			@RequestParam("delYn") String delYn) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		
		log.debug("### ntcTypeCd is " + ntcTypeCd);
		log.debug("### delYn is " + delYn);
		
		int total = noticeBO.getNoticeListCount(ntcTypeCd, delYn);
		paginate.setPage(page);
		paginate.setTotalRows(total);
		int offset = CommonUtil.calcOffset(page, paginate.getPageSize());
		
		List<DataMap> noticeList = noticeBO.getNoticeList(ntcTypeCd, delYn, offset, paginate.getPageSize());
		
		jsonMap.put("total", total);
		jsonMap.put("paginate", paginate.getPaginateMap());
		jsonMap.put("noticeList", noticeList);
		return jsonMap;
	}
	
	@RequestMapping(value = "/form", method = RequestMethod.GET)
	public String noticeForm(Model model) {
		Notice notice = new Notice();
		model.addAttribute(notice);
		return "etc/noticeForm";
	}
	
	@RequestMapping(value = "/form", method = RequestMethod.POST)
	public String save(@ModelAttribute Notice notice) throws Exception {
		try {
			noticeBO.saveOrUpdate(notice);
		} catch (Exception e) {
			log.error(e, e);
			return "redirect:etc/noticeForm";
		}
		return "redirect:/etc/notice";
	}
}
